// 评论管理
const express = require("express")
const router = express.Router()
const shareDB = require("../../../db/share")
const journalDB = require('../../../db/journal')

// 获取所有的评论
router.get('/', async (req, res) => {
  try {
    let list1 = await shareDB.find()
    let list2 = await journalDB.find()

    list1 = list1.map(item => {
      let com = item.comment || []

      com = com?.map(art => ({ ...art?._doc, title: item.title, articleID: item._id }))

      com['title'] = item.title
      return com
    })
    list2 = list2.map(item => {
      let com = item.comment || []

      com = com?.map(art => ({ ...art?._doc, title: item.title, articleID: item._id }))

      com['title'] = item.title
      return com
    })


    res.send({
      mes: '评论',
      data: [...list1, ...list2].flat(Infinity)
    })
  } catch (error) {
    res.send({
      code: 5,
      mes: '服务器异常，请稍后再试'
    })
  }
})

// 删除评论
router.use('/delete', require('./delete'))
module.exports = router